package com.socket.nio.nio.chat.example2.server;

import com.socket.nio.nio.chat.example2.protocol.MessageCodecSharable;
import com.socket.nio.nio.chat.example2.protocol.ProcotolFrameDecoder;
import com.socket.nio.nio.chat.example2.server.handler.*;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ChatServer {
  public static void main(String[] args) {
    NioEventLoopGroup boss = new NioEventLoopGroup();
    NioEventLoopGroup worker = new NioEventLoopGroup();
//    LoggingHandler LOGGING_HANDLER = new LoggingHandler(LogLevel.DEBUG);
    MessageCodecSharable MESSAGE_CODEC = new MessageCodecSharable();
    LoginRequestMessageHandler LOGIN_HANDLER = new LoginRequestMessageHandler();
    ChatRequestMessageHandler CHAT_HANDLER = new ChatRequestMessageHandler();
    GroupCreateRequestMessageHandler GROUP_CREATE_HANDLER = new GroupCreateRequestMessageHandler();
    GroupJoinRequestMessageHandler GROUP_JOIN_HANDLER = new GroupJoinRequestMessageHandler();
    GroupMembersRequestMessageHandler GROUP_MEMBERS_HANDLER = new GroupMembersRequestMessageHandler();
    GroupQuitRequestMessageHandler GROUP_QUIT_HANDLER = new GroupQuitRequestMessageHandler();
    GroupChatRequestMessageHandler GROUP_CHAT_HANDLER = new GroupChatRequestMessageHandler();
    try {
      ServerBootstrap serverBootstrap = new ServerBootstrap();
      serverBootstrap.channel(NioServerSocketChannel.class);
      serverBootstrap.group(boss, worker);
      serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel ch) {
          ch.pipeline().addLast(new ProcotolFrameDecoder());
//          ch.pipeline().addLast(LOGGING_HANDLER);
          ch.pipeline().addLast(MESSAGE_CODEC);
          ch.pipeline().addLast(LOGIN_HANDLER);
          ch.pipeline().addLast(CHAT_HANDLER);
          ch.pipeline().addLast(GROUP_CREATE_HANDLER);
          ch.pipeline().addLast(GROUP_JOIN_HANDLER);
          ch.pipeline().addLast(GROUP_MEMBERS_HANDLER);
          ch.pipeline().addLast(GROUP_QUIT_HANDLER);
          ch.pipeline().addLast(GROUP_CHAT_HANDLER);
        }
      });
      Channel channel = serverBootstrap.bind(8080).sync().channel();
      channel.closeFuture().sync();
    } catch (InterruptedException e) {
      log.error("server error", e);
    } finally {
      boss.shutdownGracefully();
      worker.shutdownGracefully();
    }
  }

}
